import { Toast } from "antd-mobile";
import { reqSaveEvent } from "@/store/actions/apiServices";

export default class AddHomeEvent {

    static canAddHomeEvent = null

    static addBeforeInstallPromptEventListener() {
        const ready = (e) => {
            e.preventDefault();
            this.canAddHomeEvent = (e);
        };
        window.addEventListener("beforeinstallprompt", ready);
    }

    static onHandleAddHomeEventClick() {
        if (window.canAddHomeEvent) {
            // return prompt.prompt();
            window.canAddHomeEvent.prompt()
            window.canAddHomeEvent.userChoice.then((choiceResult) => {
                console.log(choiceResult.outcome)
                if (choiceResult.outcome === 'accepted') {
                    // Toast.show('Add success!');
                    window.dispatchEvent(new Event('onAppAgreeInstall'));
                    reqSaveEvent("ADD_SCREEN_SUCCESS");
                    window.canAddHomeEvent = null;
                } else {
                    // Toast.show('Not agree install short');
                    reqSaveEvent("ADD_SCREEN_NOT_AGREE");
                    window.canAddHomeEvent = null;
                }
            });
            return true;
        }
        else {
            // Toast.show("beforeinstallprompt not in window!")
            reqSaveEvent("ADD_SCREEN_NOT_SURPORT_NOW");
            return false;
        }
    }

    // 处理书签事件
    static onHandleAddFavoriteEventClick() {
        navigator.clipboard.writeText("https://www.allproductghs.online")
            .then(() => {
                console.log('Text copied to clipboard');
                Toast.show("Copy success")
            })
            .catch(err => {
                console.error('Failed to copy text:', err);
            });
    }
}